/* 
 * Copyright 2014 ShangDao.Ltd  All rights reserved.
 * SiChuan ShangDao.Ltd PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 
 * @BaListPopWindow.java  2014-9-16 下午2:25:41 - Carson
 * @author Guzhu
 * @email:muyi126@163.com
 * @version 1.0
 */

package com.android.tiango.baba.view;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.android.scsd.tiango.R;
import com.android.tiango.baba.BaBaseApplication;
import com.android.tiango.baba.util.DensityUtil;
import com.android.tiango.baba.util.StringUtils;

public class BaListPopWindow {
	private ArrayList<String> list = new ArrayList<String>();
	private Context context;
	private PopupWindow popupWindow;
	// 位置不在中心
	private int popupGravity = Gravity.NO_GRAVITY;
	private int mScreenWidth;
	private int mPopupWindowWidth;
	private int mPopupWindowHeight;

	public BaListPopWindow(Context context) {
		this.context = context;
	}

	public void setData(ArrayList<String> list) {
		this.list = list;
	}

	@SuppressLint("NewApi")
	public void showPopWindow(View view) {
		View father = LayoutInflater.from(context).inflate(R.layout.list_pop, null);
		ListView lView = (ListView) father.findViewById(R.id.lv_pop);

		GroupAdapter groupAdapter = new GroupAdapter(list);
		lView.setAdapter(groupAdapter);
		popupWindow = new PopupWindow(father, DensityUtil.dip2px(context, 150),
				LayoutParams.WRAP_CONTENT);
		popupWindow.setFocusable(true);
		// 设置允许在外点击消失
		popupWindow.setOutsideTouchable(true);
		// 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		// WindowManager windowManager = (WindowManager) context
		// .getSystemService(Context.WINDOW_SERVICE);
		// // 显示的位置为:屏幕的宽度的一半-PopupWindow的高度的一半
		// int xPos = windowManager.getDefaultDisplay().getWidth() / 2 - popupWindow.getWidth() / 2;
		// popupWindow.showAsDropDown(view, xPos, 0);
		mPopupWindowWidth = popupWindow.getWidth();
		mPopupWindowHeight = popupWindow.getHeight();
		popupWindow.showAsDropDown(view, 0, DensityUtil.dip2px(context, 40));
		mScreenWidth = BaBaseApplication.mWidth;
		// popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, mScreenWidth-mPopupWindowWidth, view.getHeight());

		lView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if (null != popupWindow) {
					popupWindow.dismiss();
				}
				if (null != popClick) {
					popClick.popOnclick(position, list.get(position));
				}
			}

		});
	}

	class GroupAdapter extends BaseAdapter {
		private ArrayList<String> list;

		public GroupAdapter(ArrayList<String> list) {
			this.list = list;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return null == list ? 0 : list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = LayoutInflater.from(context).inflate(R.layout.item_pop, null);
			TextView tv_all = (TextView) view.findViewById(R.id.tv_all);
			String msg = list.get(position);
			if (!StringUtils.isEmpty(msg)) {
				tv_all.setText(msg);
			}
			return view;
		}

	}

	public interface PopWindowsClickListener {
		public void popOnclick(int position, String msg);
	}

	private PopWindowsClickListener popClick;

	public void setPopWindowsClickListener(PopWindowsClickListener popClick) {
		this.popClick = popClick;
	}
}
